# jsb/plugs/common/plus.py
#
#

""" plugin to query the Google+ API. """

## jsb imports

from jsb.utils.lazydict import LazyDict
from jsb.utils.url import geturl2
from jsb.lib.jsbimport import _import_byfile
from jsb.lib.datadir import getdatadir
from jsb.lib.commands import cmnds
from jsb.lib.examples import examples
from jsb.imports import getjson

json = getjson()

## basic imports

import os
import logging

## plus command

def handle_plus(bot, event):
    if event.args: target = event.args[0]
    else: event.missing("userid") ; return
    credentials = _import_byfile("credentials", getdatadir() + os.sep + "config" + os.sep + "credentials.py")
    url = "https://www.googleapis.com/plus/v1/people/%s/activities/public?alt=json&pp=1&key=%s" % (target, credentials.googleclient_apikey)
    result = geturl2(url)
    data = json.loads(result)
    res = []
    for item in data['items']:
        i = LazyDict(item)
        res.append("%s - %s - %s" % (i.actor['displayName'], i['title'], item['url']))
    if res: event.reply("results: ", res, dot=" || ")
    else: event.repy("no data found")

cmnds.add("plus", handle_plus, ["OPER", "USER"])
examples.add("plus", "query activities of a userid on google+", "plus 115623252983295760522")
